
**** Main Specification
 

use "Temp/Master_5yr.dta" , clear
 *drop if year<1900

 
  * set graph off to surpress interflex plot (just used for calculated Hainmueller et al's p
 set graphics off
  
  
 * SET LABELS
 
 label var libdem_extra_vdem "Liberal democracy score (LD)"

  
	// (0) Baseline
	eststo m0: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_* 	c.libdem_extra_vdem  , absorb(year iso_n) cluster(     iso_n)



	// (1) British colony interaction 

					

					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m1: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_* 	  c.libdem_extra_vdem##i.Britain_col    , absorb(year iso_n) cluster(     iso_n)
						tempfile holding
						margins Britain_col  , dydx(libdem_extra_vdem)   post saving(`holding') noestimcheck
						
						
						clonevar _m1 = Britain_col
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						egen temp_marg_low=mean(_margin) if Britain_col==0
						egen temp_marg_high=mean(_margin) if Britain_col==1
						egen temp_p_low=mean(_pvalue) if Britain_col==0
						egen temp_p_high=mean(_pvalue) if Britain_col==1					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_high=mean(temp_marg_high)
						egen p_low=mean(temp_p_low)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_high_s=p_high
					
					* actual model					
					eststo m1: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_* 	  c.libdem_extra_vdem##i.Britain_col    , absorb(year iso_n) cluster(     iso_n)
					
					estadd scalar N_true = e(N) + e(num_singletons)
					estadd local moderator_medium =  "-"    // does not exist (since binary variable
					estadd local moderator_medium_se =  ""
					estadd local moderator_high =   "absorbed"
					estadd local moderator_high_se =  ""
	
					
						* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high
					
			
	// (2) Settler effect

	
					* calculate marginal effects and save them and joint pvalues into scalars
						eststo m1b: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_* 	   c.libdem_extra_vdem##i.eshare_easterly_binned   , absorb(year iso_n) cluster(     iso_n)
							tempfile holding
							margins eshare_easterly_binned  , dydx(libdem_extra_vdem)   post saving(`holding') noestimcheck
							
							 
 							clonevar _m1 = eshare_easterly_binned
							merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
							drop _m1 _merge
 							 
							
							
							egen temp_marg_low=mean(_margin) if eshare_easterly_binned==1
							egen temp_marg_medium=mean(_margin) if eshare_easterly_binned==2						
							egen temp_marg_high=mean(_margin) if eshare_easterly_binned==3
							
							egen temp_p_low=mean(_pvalue) if eshare_easterly_binned==1
							egen temp_p_medium=mean(_pvalue) if eshare_easterly_binned==2
							egen temp_p_high=mean(_pvalue) if eshare_easterly_binned==3				
							
							egen margin_low=mean(temp_marg_low)
							egen margin_medium=mean(temp_marg_medium)
							egen margin_high=mean(temp_marg_high)

							egen p_low=mean(temp_p_low)
							egen p_medium=mean(temp_p_medium)
							egen p_high=mean(temp_p_high)
												
							scalar p_low_s=p_low
							scalar p_medium_s=p_medium
							scalar p_high_s=p_high
		
	 
		
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real libdem_extra_vdem  eshare_easterly l1_int_war_all_PRIO l1_civ_war_all_PRIO    P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged l1_gov_change X_*, fe(year iso_n) cl(iso_n) nbins(4)
					scalar   l_P_wald =  r(pwald)
					
					* actual model 
					eststo m1b: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_* 	   c.libdem_extra_vdem##i.eshare_easterly_binned   , absorb(year iso_n) cluster(     iso_n)
					
					estadd scalar N_true = e(N) + e(num_singletons)
					estadd scalar P_wald = l_P_wald
					estadd local moderator_medium =  "absorbed"
					estadd local moderator_medium_se =  ""
					estadd local moderator_high =   "absorbed"
					estadd local moderator_high_se =  ""

					

 			 
  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.2f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
					
		
  
	// (3) Eth_frac_Fractionalization Querol
	
					* calculate marginal effects and save them and joint pvalues into scalars
 					eststo m2: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_* c.libdem_extra_vdem c.libdem_extra_vdem##i.eth_frac_Querol_binned      , absorb(year iso_n) cluster(   iso_n)
							tempfile holding
							margins eth_frac_Querol_binned  , dydx(libdem_extra_vdem)   post saving(`holding') noestimcheck
							clonevar _m1 = eth_frac_Querol_binned
							merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
							drop _m1 _merge
							
							 
							
							
							egen temp_marg_low=mean(_margin) if eth_frac_Querol_binned==0
							egen temp_marg_medium=mean(_margin) if eth_frac_Querol_binned==1						
							egen temp_marg_high=mean(_margin) if eth_frac_Querol_binned==2
							
							egen temp_p_low=mean(_pvalue) if eth_frac_Querol_binned==0
							egen temp_p_medium=mean(_pvalue) if eth_frac_Querol_binned==1
							egen temp_p_high=mean(_pvalue) if eth_frac_Querol_binned==2					
							
							egen margin_low=mean(temp_marg_low)
							egen margin_medium=mean(temp_marg_medium)
							egen margin_high=mean(temp_marg_high)

							egen p_low=mean(temp_p_low)
							egen p_medium=mean(temp_p_medium)
							egen p_high=mean(temp_p_high)
												
							scalar p_low_s=p_low
							scalar p_medium_s=p_medium
							scalar p_high_s=p_high	
							
							
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real libdem_extra_vdem  eth_frac_Querol l1_int_war_all_PRIO l1_civ_war_all_PRIO   P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged l1_gov_change X_*, fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)

					* actual model 	
 					eststo m2: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_* c.libdem_extra_vdem c.libdem_extra_vdem##i.eth_frac_Querol_binned      , absorb(year iso_n) cluster(   iso_n)
					
					estadd scalar N_true = e(N) + e(num_singletons)
					estadd scalar P_wald = l_P_wald
					estadd local moderator_medium =  "absorbed"
					estadd local moderator_medium_se =  ""
					estadd local moderator_high =   "absorbed"
					estadd local moderator_high_se =  ""
					
					
  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.2f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
										
	
	
							

	// (4) Access to ODA

					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m4: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare      cr_market_accessXBOEinv X_* c.libdem_extra_vdem c.libdem_extra_vdem##i.S_g5_unw_alliance_abs_binned     , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins S_g5_unw_alliance_abs_binned  , dydx(libdem_extra_vdem)   post saving(`holding')
						clonevar _m1 = S_g5_unw_alliance_abs_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if S_g5_unw_alliance_abs_binned==0
						egen temp_marg_medium=mean(_margin) if S_g5_unw_alliance_abs_binned==1						
						egen temp_marg_high=mean(_margin) if S_g5_unw_alliance_abs_binned==2
						
						egen temp_p_low=mean(_pvalue) if S_g5_unw_alliance_abs_binned==0
						egen temp_p_medium=mean(_pvalue) if S_g5_unw_alliance_abs_binned==1
						egen temp_p_high=mean(_pvalue) if S_g5_unw_alliance_abs_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
	
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real libdem_extra_vdem   S_g5_unw_alliance_abs l1_int_war_all_PRIO l1_civ_war_all_PRIO   P_ind_total_f_realshare   S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged l1_gov_change X_* , fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)

					* actual model 	
					eststo m4: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change  P_ind_total_f_realshare      cr_market_accessXBOEinv X_* c.libdem_extra_vdem c.libdem_extra_vdem##i.S_g5_unw_alliance_abs_binned     , absorb(year iso_n) cluster(   iso_n)
						
						* add statistics to table
						scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.S_g5_unw_alliance_abs_binned] / _se[1.S_g5_unw_alliance_abs_binned]) ) )
						scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.S_g5_unw_alliance_abs_binned] / _se[2.S_g5_unw_alliance_abs_binned]) ) )
						estadd scalar N_true = e(N) + e(num_singletons)
						estadd scalar P_wald = l_P_wald
						estadd local	moderator_medium  	"`= cond(pval_moderator_medium<0.01,"`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''*",  "`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''")))'"					
						estadd local	moderator_high  	"`= cond(pval_moderator_high<0.01,"`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''***", 		cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''*",  "`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''")))'"
						estadd scalar moderator_medium_se = _se[1.S_g5_unw_alliance_abs_binned] 
						estadd scalar moderator_high_se =  _se[2.S_g5_unw_alliance_abs_binned] 
						
						
 
						
  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.2f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
										
	
							
 	// (5) Access to credit

	
	
					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m5: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change   X_*            S_g5_unw_alliance_abs  P_ind_total_f_realshare   		c.libdem_extra_vdem##i.cr_market_accessXBOEinv_binned     , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins cr_market_accessXBOEinv_binned  , dydx(libdem_extra_vdem)   post saving(`holding')
						clonevar _m1 = cr_market_accessXBOEinv_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if cr_market_accessXBOEinv_binned==0
						egen temp_marg_medium=mean(_margin) if cr_market_accessXBOEinv_binned==1						
						egen temp_marg_high=mean(_margin) if cr_market_accessXBOEinv_binned==2
						
						egen temp_p_low=mean(_pvalue) if cr_market_accessXBOEinv_binned==0
						egen temp_p_medium=mean(_pvalue) if cr_market_accessXBOEinv_binned==1
						egen temp_p_high=mean(_pvalue) if cr_market_accessXBOEinv_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
						
	
	
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real libdem_extra_vdem   cr_market_accessXBOEinv l1_int_war_all_PRIO l1_civ_war_all_PRIO      S_g5_unw_alliance_abs P_ind_total_f_realshare    l1_drought_affected_merged l1_gov_change X_*, fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)

 					* actual model 	
					eststo m5: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change   	X_*            S_g5_unw_alliance_abs  P_ind_total_f_realshare   		c.libdem_extra_vdem##i.cr_market_accessXBOEinv_binned     , absorb(year iso_n) cluster(   iso_n)
 
						* add statistics to table
						scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.cr_market_accessXBOEinv_binned] / _se[1.cr_market_accessXBOEinv_binned]) ) )
						scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.cr_market_accessXBOEinv_binned] / _se[2.cr_market_accessXBOEinv_binned]) ) )
						estadd scalar N_true = e(N) + e(num_singletons)
						estadd scalar P_wald = l_P_wald
						estadd local	moderator_medium  "`= cond(pval_moderator_medium<0.01,"`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''*",  "`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''")))'"					
						estadd local	moderator_high  "`= cond(pval_moderator_high<0.01,"`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''***", 		cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''*",  "`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''")))'"
						estadd scalar moderator_medium_se = _se[1.cr_market_accessXBOEinv_binned] 
						estadd scalar moderator_high_se =  _se[2.cr_market_accessXBOEinv_binned] 
						

  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.2f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
										
							

	
	// (6) Interaction with resource prices 
	
					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m3: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change    S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_* c.libdem_extra_vdem  c.libdem_extra_vdem##i.P_ind_total_f_realshare_binned    , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins P_ind_total_f_realshare_binned  , dydx(libdem_extra_vdem)   post saving(`holding')
						clonevar _m1 = P_ind_total_f_realshare_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if P_ind_total_f_realshare_binned==0
						egen temp_marg_medium=mean(_margin) if P_ind_total_f_realshare_binned==1						
						egen temp_marg_high=mean(_margin) if P_ind_total_f_realshare_binned==2
						
						egen temp_p_low=mean(_pvalue) if P_ind_total_f_realshare_binned==0
						egen temp_p_medium=mean(_pvalue) if P_ind_total_f_realshare_binned==1
						egen temp_p_high=mean(_pvalue) if P_ind_total_f_realshare_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
	
	
		
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real libdem_extra_vdem   P_ind_total_f_realshare l1_int_war_all_PRIO l1_civ_war_all_PRIO      S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged l1_gov_change X_*, fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)
	
	
					* actual model 	
					eststo m3: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged l1_gov_change    S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_* c.libdem_extra_vdem  c.libdem_extra_vdem##i.P_ind_total_f_realshare_binned    , absorb(year iso_n) cluster(   iso_n)
					
						* add statistics to table
						scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.P_ind_total_f_realshare_binned] / _se[1.P_ind_total_f_realshare_binned]) ) )
						scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.P_ind_total_f_realshare_binned] / _se[2.P_ind_total_f_realshare_binned]) ) )
						estadd scalar N_true = e(N) + e(num_singletons)
						estadd scalar P_wald = l_P_wald
						estadd local	moderator_medium  "`= cond(pval_moderator_medium<0.01,"`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''*",  "`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''")))'"					
						estadd local	moderator_high  "`= cond(pval_moderator_high<0.01,"`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''***", 	cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''*",  "`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''")))'"
						estadd scalar moderator_medium_se = _se[1.P_ind_total_f_realshare_binned] 
						estadd scalar moderator_high_se =  _se[2.P_ind_total_f_realshare_binned] 
						
  							
							
							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.2f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.2f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.2f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
										
		
  
		 
	 esttab   m1 m1b m2   m4 m5 m3  using "Output/Tables/Main/Table3.rtf", replace style(tex) cells(b(star fmt(2)) se(par fmt(2)) P_wald(  fmt(2))) stats(margin_low margin_medium margin_high moderator_medium moderator_medium_se moderator_high moderator_high_se  N_true r2_a    P_wald, fmt(2 2 2 2  2  2  2  0 2 2 ) labels("Marginal effect (moderator low)"    "Marginal effect (moderator medium)" "Marginal effect (moderator high)" "Moderator - medium" " " "Moderator - high" " " "Observations" "Adjusted  R^2 " "Xu's Wald test")) legend label   starlevels(* 0.1 ** 0.05 *** 0.01) drop(_cons l1_int_war_all_PRIO l1_gov_change  l1_civ_war_all_PRIO l1_drought_affected_merged  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_*)
	  
	 esttab    m1 m1b m2   m4 m5  m3   using "Output/Tables/Main/Table3.tex", replace style(tex) cells(b(star fmt(2)) se(par fmt(2)) P_wald(  fmt(2))) stats(margin_low margin_medium margin_high moderator_medium   moderator_high    N_true r2_a    P_wald, fmt(2 2 2    2  2  0 2 2 ) labels("Marginal effect (moderator low)"    "Marginal effect (moderator medium)" "Marginal effect (moderator high)" "Moderator - medium"  "Moderator - high"  "Observations" "Adjusted  R^2 " "HMX Wald test")) legend label   starlevels(* 0.1 ** 0.05 *** 0.01) drop(_cons l1_int_war_all_PRIO l1_gov_change  l1_civ_war_all_PRIO l1_drought_affected_merged  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_*)


	set graphics on
